<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\AuthController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\MenuController;
use App\Http\Controllers\Admin\DeptController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\DictController;
use App\Http\Controllers\Admin\DictItemController;
use App\Http\Controllers\Admin\LogController;
use App\Http\Controllers\Admin\NoticeController;


Route::get('/', function () {
    return view('welcome');
});



Route::prefix('api/v1')->group(function () {



    // 不需要 JWT 鉴权的 auth 路由
    Route::prefix('auth')->controller(AuthController::class)->group(function () {
        Route::post('init', 'init');       // 初始化 admin 账号
        Route::get('captcha', 'captcha');  // 获取验证码
        Route::post('login', 'login');     // 登录
        Route::post('refresh-token', 'refreshToken'); // 刷新令牌
    });

    // 需要 JWT 鉴权的路由
    Route::middleware(['jwt_auth', 'log'])->group(function () {
        Route::prefix('auth')->controller(AuthController::class)->group(function () {
            Route::delete('logout', 'logout');
        });

        Route::get('users/options', [UserController::class, 'options']);
        Route::put('users/{id}/password/reset', [UserController::class, 'passwordReset']);
        Route::get('users/me', [UserController::class, 'me']);  // 获取当前登录用户信息
        Route::resource('users', UserController::class);

        Route::get('menus/routes', [MenuController::class, 'routes']);  // 获取路由菜单列表
        Route::get('menus/options', [MenuController::class, 'options']);  // 获取路由菜单列表
        Route::resource('menus', MenuController::class);

        Route::get('dept/options', [DeptController::class, 'options']);  // 获取部门下拉选项
        Route::resource('dept', DeptController::class);

        Route::get('roles/{roleId}/menuIds', [RoleController::class, 'menuIds']); // 获取已经选择的权限菜单
        Route::put('roles/{roleId}/menus', [RoleController::class, 'menus']); // 获取已经选择的权限菜单
        Route::get('roles/options', [RoleController::class, 'options']);
        Route::resource('roles', RoleController::class);

        Route::resource('dicts', DictController::class);

        Route::get('dicts/{dictCode}/items/options', [DictItemController::class, 'options']);
        Route::resource('dicts.items', DictItemController::class);

        Route::resource('logs', LogController::class)->only(['index'])->withoutMiddleware(['log']);

        Route::put('notices/read-all', [NoticeController::class, 'readAll']); // 全部标记为已读
        Route::put('notices/{id}/publish', [NoticeController::class, 'publish']); // 发布公告
        Route::put('notices/{id}/revoke', [NoticeController::class, 'revoke']); // 撤销公告
        Route::get('notices/my-page', [NoticeController::class, 'myPage']); // 我的公告分页列表
        Route::get('notices/{id}/read', [NoticeController::class, 'read']); // 获取公告并标记公告为已读
        Route::resource('notices', NoticeController::class);
    });

    // 其他先只返回 json
    Route::get('logs/visit-trend', function () {
        return json_encode(json_decode('{"code":"00000","data":{"dates":["2025-10-18","2025-10-19","2025-10-20","2025-10-21","2025-10-22","2025-10-23","2025-10-24"],"pvList":[972,1019,3770,4457,5075,4293,2435],"ipList":[137,111,334,405,394,363,222]},"msg":"一切ok"}'));
    });


    Route::get('logs/visit-stats', function () {
        return json_encode(json_decode('{"code":"00000","data":{"todayUvCount":222,"totalUvCount":4674,"uvGrowthRate":16.08,"todayPvCount":2435,"totalPvCount":77162,"pvGrowthRate":32.82},"msg":"一切ok"}'));
    });
});
